package com.atguigu.chap4;

public class VolatileAndInteger {
    //添加volatile关键字
    private static volatile Integer flag = 1;

    //可见性测试
    public static void main(String[] args) throws InterruptedException {
        new Thread(()->{
            System.out.println(Thread.currentThread().getName() + "  ===========>  flag=" + flag);
            while(flag == 1){}
            System.out.println(Thread.currentThread().getName() + "  ===========>  flag=" + flag);
        },"t1").start();

        System.out.println(Thread.currentThread().getName() + "  ===========>  flag=" + flag);
        Thread.sleep(500);
        flag = 2;
        System.out.println(Thread.currentThread().getName() + "  ===========>  flag=" + flag);
    }
}
